package cm

import (
	"WZ_law-api/Main"

	"github.com/ethereum/go-ethereum/common"

	"github.com/FISCO-BCOS/go-sdk/client"
	"github.com/FISCO-BCOS/go-sdk/conf"

	"log"
)

type ClientManager struct {
	Client   *client.Client
	Contract *Main.MainSession
}

const Config string = "config.toml"
const Contractaddr string = "0xF841A58EF861Ae60E4025947a6BB3F108339710D"

func NewClientManager() *ClientManager {
	var configs, err = conf.ParseConfigFile(Config) //读取配置文件
	if err != nil {
		log.Fatalln("ParseConfig err:", err)
	}
	config := &configs[0]
	c, err := client.Dial(config) //连接区块链
	if err != nil {
		log.Fatalln("client dial err:", err)
	}

	instance, err := Main.NewMain(common.HexToAddress(Contractaddr), c) //新建合约实例
	if err != nil {
		log.Fatalln("NewMain contract err:", err)
	}
	//创建合约调用对象
	MainSession := &Main.MainSession{Contract: instance, CallOpts: *c.GetCallOpts(), TransactOpts: *c.GetTransactOpts()}
	//对象封装
	cm := &ClientManager{
		Client:   c,
		Contract: MainSession,
	}
	return cm
}
